// var publicurl = "http://cdb.ydcgzxny.com/client/cdb/gzhAPP_ajax/";
var publicurl = "http://192.168.0.5:8090";
document.write('<script type="text/javascript" src="../../script/layer.js"></script>');
document.write('<script src="../../script/axios.min.js"></script>');
$("head").append("<link>");
css = $("head").children(":last");
css.attr({
	rel: "stylesheet",
	type: "text/css",
	href: "../../css/layer.css"
});
// 设置语言

	let lang ;
	lang = navigator.language || navigator.userLanguage;
	lang = lang.substr(0, 2);
	let iflang = localStorage.getItem('lang');
	if (!iflang) {
		localStorage.setItem('lang', lang);
	}else{
		lang = iflang
	}
	const i18n = new VueI18n({
		locale: lang, 
		messages: {
			en: langEn(),
			zh: langZh()
		}
	})


//检测登录状态
function checkLogin(callback) {
	$.ajax({
		type: "post",
		url: publicurl + "Login/Check.asp",
		dataType: "json",
		success: function(ret) {
			if (ret.code == 1) {
				callback && callback(true);
			} else if (ret.code == 2) {
				callback && callback(false);
			}
		},
		error: function(err) {
			myAlert(JSON.stringify(err), "系统提示");
		}
	})
};

function myAlert(content, title, callback) { //提示框
	if (title == '' || !title || title == 'undefined') {
		title = "温馨提示";
	};
	layer.open({
		title: title,
		content: content,
		btn: "确定",
		yes: function(index) {
			layer.close(index);
			callback && callback();
		}
	})
};

function myConfirm(content, callback) {
	layer.open({
		title: "温馨提示",
		content: content,
		btn: ['确定', '取消'],
		yes: function(index) {
			layer.close(index);
			callback && callback();
		}
	})
};


function myToast(content, location) {
	layer.open({
		content: content,
		skin: 'msg',
		time: 2 //2秒后自动关闭
	});
};

function postAjax(url, type, data, callback) {
	myloading();
// 请求前拦截器
axios.interceptors.request.use(
   // 在发送请求之前做些什么
  config => {
    const token = sessionStorage.getItem('token')
    if (token ) { // 判断是否存在token，如果存在的话，则每个http header都加上token
      config.headers.token = token  //请求头加上token
    }
    return config
  },
  err => {
    return Promise.reject(err)
  })
	axios({
		method: type,
		url:publicurl + url,
		async:true,
		dataType: 'json',
		contentType: 'application/json',
		data: data
	}).then(function(res) {
		console.log(res)
		if (res.data.code == 1) {
			callback && callback(res);
		} else if (res.data.code == 2) {
			myAlert(res.data.message, "温馨提示", function() {
				window.location.href = "../Login/";
			});
		}
		else if (res.data.code == 3) {
			myAlert(res.data.message, "温馨提示", function() {
				window.location.href = "../Login/";
			});
		} 
		else {
			myAlert(res.data.message?res.data.message:res.data.msg);
		};
		closeloading()
	}).catch((error) => {
		console.log(error)
						console.log(url + "error:" + JSON.stringify(error));
						closeloading();
						myAlert(JSON.stringify(JSON.stringify(error.message)), "系统提示");
					})
	
};

function myloading() {
	$("body").append(
		"<div id='load' style='position:fixed;width:100%;height:100%;top:0;left:0;background:rgba(0,0,0,0);z-index:999;'>" +
		"<div style='position:absolute;width:200px;height:200px;top:50%;left:50%;margin-top:-100px;margin-left:-100px;background:rgba(0,0,0,.5);border-radius:10px;text-align:center;line-height:260px;'><img src='../../image/load.gif' style='width:40%;height:40%;'/></div>" +
		"</div>");
		$('#load').on('touchmove',function(e){
			 e.stopPropagation()
       e.preventDefault();
  });

};

function closeloading() {
	$("#load").remove();
	//  $('body').on('touchmove',function(e){
 //      e.stopPropagation()
 // });
};


function myUpLoad(callback) {
	window.onscroll = function() {
		var sTop = document.documentElement.scrollTop + document.body.scrollTop + $(window).height();
		var sHeight = document.body.scrollHeight;
		// console.log(sTop,sHeight)
		// var sHeight = document.body.scrollHeight - 320 - 100;
		if (sTop  == sHeight) {
			console.log("触底了")
			callback && callback();
		}
		//		if(page<=maxpage){
		//			var sTop=document.documentElement.scrollTop+document.body.scrollTop+$(window).height();
		//			var sHeight=document.body.scrollHeight-320-jian;
		//			if(sTop >= sHeight){
		//				$(".nomore").hide();
		//				callback && callback(true);
		//			}
		//		}else{
		//			callback && callback(false);
		//			$(".nomore").show();
		//		}
	}
};

function checkMobile(Mobile) {
	if (!(/^1[3|4|5|8|7|9][0-9]\d{8}$/.test(Mobile))) {
		return false;
	} else {
		return true;
	}
};

function isnull(val) {
	var str = val.replace(/(^\s*)|(\s*$)/g, ''); //去除空格;
	if (str == '' || str == undefined || str == null) {
		return true;
		//          console.log('空');
	} else {
		return false;
		//          console.log('非空');
	}
};

//跳转页面
function goNewPage(url) {
	window.location.href = url;
};

//获取url传的参数
function getCanshu(url) {
	if (url.indexOf("?") != -1) {
		var str = url.substr(1);
		str = str.replace(/&/g, '","');
		str = str.replace(/=/g, '":"');
		str = '{"' + str + '"}';
		var result = JSON.parse(str);
	} else {
		var result = "未接收到参数!";
	}
	return result;
}

//下拉刷新

function Slide(dom){
    this.start_y=null;
    this.end_y=null;
    this.render(dom);
}
Slide.prototype={
    // 动态创建下拉刷新特效
    render:function(dom){
        var body = document.getElementsByTagName('body');
        var newItem = document.createElement('div');
        newItem.className = 'slide-wrap';
        newItem.innerHTML = '<div class="slided">正在刷新。。。</div>'+'<div class="sliding">下拉刷新</div>';
        body[0].insertBefore(newItem,dom[0]);
        var dom2 = document.getElementsByClassName('slide-wrap');
        this.slide_start(dom2);
    },
    slide_start:function(dom2){
        var _this = this;
            document.addEventListener('touchstart',function(evt){
                 var touch = evt.touches[0]; //获取第一个触点
                 _this.start_y = Number(touch.pageY); 
            });
        _this.slide_move(dom2);
    },
    slide_move:function(dom2){
            var _this = this;
            document.addEventListener('touchmove',function(evt){
                 dom2[0].childNodes[0].style.display='none';
                 var touch = evt.touches[0]; //获取第一个触点
                 _this.end_y = Number(touch.pageY) - _this.start_y; 
                 if(_this.end_y > 5){
                     dom2[0].childNodes[1].style.display = 'block';
                     dom2[0].childNodes[1].style.height = _this.end_y +'px';
                     dom2[0].childNodes[1].style.lineHeight = _this.end_y +'px';
                 }
            });
        _this.slide_end(dom2);
    },
    slide_end:function(dom2){
        var _this = this;
            document.addEventListener('touchend',function(evt){
                if(_this.end_y > 100){
                    dom2[0].childNodes[1].style.display='none';
                    dom2[0].childNodes[0].style.display='block';
                       // console.log('开始刷新');
                    setTimeout(function(){
                        dom2[0].childNodes[0].style.display='none';
												_this.myPullDown()
												_this.end_y = 0;
                    },10);
                }else{
                     dom2[0].childNodes[1].style.display = 'none';
                }
            });
    }
}


